import axios from "axios";
import apiURL from './api';

axios.defaults.timeout = 10000;
axios.defaults.baseURL = apiURL;
axios .defaults.headers = {
    'Content-Type' : 'application/json;charset=UTF-8'
}

axios.defaults.transformRequest = (data)  => {
    data = JSON.stringify(data);
    return data
}


// 路由器拦截
axios.interceptors.request.use(config =>{
    if(config.method === 'get'){
        return config
    }
    // 判断是否有token
    // const token = localStorage.getItem('token')
    // if(token){
    //     config.headers.token = token;
    // }
    return config;
},error => {
    return Promise.reject(error.response);
})

// 路由器响应
axios.interceptors.response.use(response =>{
    if(response.data.code !== 200) {
        console.log(response.data.code)
        return
    }else{
       return response.data;
    }
},error => {
    return Promise.reject(error.response)
})


export default axios;